// 如何处理整个报文
#pragma once

#include <iostream>
#include "Protocal.hpp"

enum
{
    Div_Zero = 1,
    Mod_Zero,
    unKnow
};
class ServerCal
{
public:
    ServerCal()
    {
    }

    Response CalculatorHelper(const Request &req)
    {
        Response res(0, 0);
        switch (req._op)
        {
        case '+':
            res._result = req._x + req._y;
            break;
        case '-':
            res._result = req._x - req._y;
            break;
        case '*':
            res._result = req._x * req._y;
            break;
        case '/':
        {
            if (req._y == 0)
            {
                res._code = Div_Zero;
                break;
            }
            res._result = req._x / req._y;
            break;
        }
        case '%':
        {
            if (req._y == 0)
            {
                res._code = Mod_Zero;
                break;
            }
            res._result = req._x % req._y;
            break;
        }
        default:
            res._code = unKnow;
            break;
        }
        return res;
    }

    std::string Calculator(std::string &package)
    {
        // 此时肯定是收到了已经封装好报头的消息
        // 所以第一步是解析报头
        Request req;
        std::string content;
        //std::cout << "content:" << content << "xxxxx" << std::endl;
        int r = Decode(package, &content);
        //std::cout << "解码后content:" << content << "xxxxx" << std::endl;
        if (!r)
            return "";
        // 进行反序列化
        r = req.Deserialize(content);
        if (!r)
            return "";
        // 进行数据处理
        Response res = CalculatorHelper(req);
        // 序列化
        content = "";
        res.Serialize(&content);
        // 封装报头
        content = Encode(content);
      //  std::cout << "构造后的响应content:" << content << "xxxxx" << std::endl;

        return content;
    }

    ~ServerCal()
    {
    }
};